# Maintainer: Jan Alexander Steffens (heftig) <heftig@archlinux.org>
# Contributor: Daniel Micay <danielmicay@gmail.com>
# Contributor: Mladen Pejakovic <pejakm@gmail.com>

pkgbase=libxkbcommon
pkgname=(libxkbcommon libxkbcommon-doc libxkbcommon-x11)
pkgver=1.3.1
pkgrel=1
pkgdesc="Keymap handling library for toolkits and window systems"
url="https://xkbcommon.org/"
arch=(x86_64)
license=(custom)
makedepends=(xkeyboard-config libxml2 libxcb wayland wayland-protocols doxygen
             git meson xorgproto)
checkdepends=(xorg-server-xvfb libgl)
_commit=88222c8d40543c29af781f7d7e25d202ff40e4b7  # tags/xkbcommon-1.3.1^0
source=("git+https://github.com/xkbcommon/libxkbcommon#commit=$_commit")
sha256sums=('SKIP')

pkgver() {
  cd $pkgbase
  git describe --tags | sed 's/^xkbcommon-//;s/-/+/g'
}

prepare() {
  cd $pkgbase
}

build() {
  arch-meson $pkgbase build
  meson compile -C build
}

check() {
  xvfb-run -s '-nolisten local' meson test -C build --print-errorlogs
}

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

package_libxkbcommon() {
  depends=(xkeyboard-config libxml2)
  optdepends=('libxkbcommon-x11: xkbcli interactive-x11'
              'wayland: xkbcli interactive-wayland')
  provides=(libxkbcommon.so libxkbregistry.so)

  meson install -C build --destdir "$pkgdir"
  install -Dt "$pkgdir/usr/share/licenses/$pkgname" -m644 $pkgbase/LICENSE

  _pick libxkbcommon-doc "$pkgdir"/usr/share/doc

  _pick libxkbcommon-x11 "$pkgdir"/usr/lib/*x11*
  _pick libxkbcommon-x11 "$pkgdir"/usr/lib/pkgconfig/*x11*
  _pick libxkbcommon-x11 "$pkgdir"/usr/lib/xkbcommon/*x11*
  _pick libxkbcommon-x11 "$pkgdir"/usr/include/xkbcommon/*x11*
  _pick libxkbcommon-x11 "$pkgdir"/usr/share/man/man1/*x11*
}

package_libxkbcommon-doc() {
  pkgdesc="API documentation for libxkbcommon"

  mv libxkbcommon-doc/* "$pkgdir"
  install -Dt "$pkgdir/usr/share/licenses/$pkgname" -m644 $pkgbase/LICENSE
}

package_libxkbcommon-x11() {
  pkgdesc="Keyboard handling library using XKB data for X11 XCB clients"
  depends=(libxkbcommon libxkbcommon.so libxcb)
  provides=(libxkbcommon-x11.so)

  mv libxkbcommon-x11/* "$pkgdir"
  install -Dt "$pkgdir/usr/share/licenses/$pkgname" -m644 $pkgbase/LICENSE
}
